package com.ioftv.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ioftv.entity.UserMenu;
import com.ioftv.mapper.UserMenuMapper;
import com.ioftv.service.UserMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户菜单权限Service实现类
 */
@Service
public class UserMenuServiceImpl extends ServiceImpl<UserMenuMapper, UserMenu> implements UserMenuService {

    @Autowired
    private UserMenuMapper userMenuMapper;

    @Override
    public List<Long> getMenuIdsByUserId(Long userId) {
        return userMenuMapper.selectMenuIdsByUserId(userId);
    }

    @Override
    public void assignMenusToUser(Long userId, List<Long> menuIds) {
        // 先删除用户原有的菜单权限
        userMenuMapper.deleteByUserId(userId);
        
        // 再批量插入新的菜单权限
        if (menuIds != null && !menuIds.isEmpty()) {
            userMenuMapper.insertBatch(userId, menuIds);
        }
    }
}